#include "bd_gps.h"
#include "usart.h"
#include "ble.h"
#include "string.h"

#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include "key.h"
#include <stdlib.h>

#include <stdio.h>

struct BD_GPS_STRUCT bd_gps_s;
extern enum BLE_STATUS_ENUM ble_status;
extern struct BD_GPS_STRUCT bd_gps_car_s;

const char GPS_DATA[] = "\
$GNGGA,143753.000,3811.8703,N,11432.6525,E,1,17,0.80,76.8,M,-14.5,M,,*69\r\n\
$GPGSA,A,3,06,07,03,30,22,21,19,01,,,,,1.12,0.80,0.78*0C\r\n\
$BDGSA,A,3,03,02,13,01,08,07,25,05,04,,,,1.12,0.80,0.78*1D\r\n\
$GPGSV,4,1,14,14,76,322,28,28,65,321,,01,60,050,25,17,41,286,28*7D\r\n\
$GPGSV,4,2,14,21,36,047,24,22,36,100,38,30,36,218,47,03,29,129,45*7B\r\n\
$GPGSV,4,3,14,44,23,241,,19,21,273,31,07,17,189,46,08,16,077,*7E\r\n\
$GPGSV,4,4,14,06,09,219,44,193,,,*72\r\n\
$BDGSV,3,1,09,07,74,121,40,08,61,195,43,03,46,187,41,25,42,189,50*6A\r\n\
$BDGSV,3,2,09,13,37,206,43,01,36,137,42,02,35,223,41,04,26,121,35*64\r\n\
$BDGSV,3,3,09,05,17,246,40*56\r\n\
$GNRMC,143753.000,A,3811.8703,N,11432.6525,E,0.015,158.55,311220,,,A*4A\r\n\
$GNVTG,158.55,T,,M,0.015,N,0.028,K,A*21\r\n\
";

void StartBDTask(void const *argument)
{
    char str[128];
    bd_gps_s.status = BD_GPS_STATUS_INVAILD;
    
    bd_uart_init();
    while(1)
    {
        if(HAL_GPIO_ReadPin(BLE_STATUS_GPIO_Port,BLE_STATUS_Pin) == GPIO_PIN_RESET)
        {
            ble_status = BLE_CONNECTING;       
        }else{
            ble_status = BLE_CONNECTED;
        }
        

        //osDelay(500); 
        send_bd_gps_status();
        osDelay(1000); 

        if(bd_gps_car_s.status == BD_GPS_STATUS_VAILD && bd_gps_s.status == BD_GPS_STATUS_VAILD)
        {
            sprintf(str,"%f,%f,%f,%f",bd_gps_car_s.lng,bd_gps_car_s.lat,bd_gps_s.lng,bd_gps_s.lat);
             
        }else if(bd_gps_car_s.status == BD_GPS_STATUS_VAILD && bd_gps_s.status == BD_GPS_STATUS_INVAILD)
        {
            sprintf(str,"%f,%f,V,V",bd_gps_car_s.lng,bd_gps_car_s.lat);
        }else if(bd_gps_car_s.status == BD_GPS_STATUS_INVAILD && bd_gps_s.status == BD_GPS_STATUS_VAILD)
        {
            sprintf(str,"V,V,%f,%f",bd_gps_s.lng,bd_gps_s.lat); 
        }else{
            sprintf(str,"V,V,V,V"); 
        }
        
        printf(str); 

        //DB_GPS_recv((uint8_t *)GPS_DATA,strlen(GPS_DATA));
    }
}

//$GNRMC,143753.000,A,3811.8703,N,11432.6525,E,0.015,158.55,311220,,,A*4A

void DB_GPS_recv(uint8_t *data,uint16_t length)
{
    char temp[16],str[128];
    char *rmc_p,*status_p,*lng_p,*lat_p,*temp_p;
    data[length] = '\0';
    rmc_p = strstr((const char *)data,"$GNRMC,");
    if(rmc_p != NULL)
    {
        rmc_p += 7;
        status_p = strstr((const char *)rmc_p,",");
        status_p += 1;
        if((*status_p) == 'A')
        {
            bd_gps_s.status = BD_GPS_STATUS_VAILD;
            //==============================================//
            lat_p = strstr((const char *)status_p,",");  //γ��
            lat_p += 1;
            temp_p = strstr((const char *)lat_p,",");
            
            memcpy(temp,lat_p,temp_p-lat_p);
            temp[temp_p-lat_p] = '\0';
            bd_gps_s.lat = strtod(temp,NULL);
            
            lat_p = strstr((const char *)lat_p,",");  
            lat_p += 1;
            if((*lat_p) == 'N')
            {   
                bd_gps_s.lat_ns = LAT_N;
                bd_gps_s.lat_char = 'N';
            }else{
                bd_gps_s.lat_ns = LAT_S;
                bd_gps_s.lat_char = 'S';
            }
            
            //==============================================//
            lng_p = strstr((const char *)lat_p,",");  
            lng_p += 1;
            temp_p = strstr((const char *)lng_p,",");
            
            memcpy(temp,lng_p,temp_p-lng_p);
            temp[temp_p-lng_p] = '\0';
            bd_gps_s.lng = strtod(temp,NULL);
            
            lng_p = strstr((const char *)lng_p,",");  
            lng_p += 1;
            if((*lng_p) == 'E')
            {   
                bd_gps_s.lng_ew = LNG_E;
                bd_gps_s.lng_char = 'E';
            }else{
                bd_gps_s.lng_ew = LNG_W;
                bd_gps_s.lng_char = 'W';
            }
            
            bd_gps_s.lat = (double)((int)bd_gps_s.lat/100) + (bd_gps_s.lat-((int)bd_gps_s.lat/100)*100)/60;
            bd_gps_s.lng = (double)((int)bd_gps_s.lng/100) + (bd_gps_s.lng-((int)bd_gps_s.lng/100)*100)/60;
            
            //sprintf(str,"%f\t%f\r\n",bd_gps_s.lat,bd_gps_s.lng);
            //printf((uint8_t *)str);
            
            
        }else{
            bd_gps_s.status = BD_GPS_STATUS_INVAILD;
        }
        
        
    }else{
        bd_gps_s.status = BD_GPS_STATUS_ERROR;
    }

    
}